<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        // 普通创建一个对象
        var obj = {
            name: "孙悟空",
            age: 18,
            gender: "男",
            sayName: function() {
                alert(this.name);
            }
        }



        // 使用工厂方法创建对象
        // 通过该方法可以大批量创建对象
        function createPerson(name, age, gender) {
            // 创建一个对象
            var obj = new Object();

            // 向对象中添加属性
            obj.name = name;
            obj.age = age;
            obj.gender = gender;
            obj.sayName = function() {
                    alert(this.name);
                }
                // 将新的对象返回
            return obj;
        }

        var obj1 = createPerson("孙悟空", 20, "男");
        var obj2 = createPerson("猪八戒", 20, "男");
        var obj3 = createPerson("白骨精", 18, "女");

        console.log(obj1);
        console.log(obj2);
        obj3.sayName();
    </script>
</body>

</html>